/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package libraryproject;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JPanel;
import user_client.LibrarianUserClient;
import user_client.NormalUserClient;

/**
 *
 * @author pawel
 */
public class MainFrame extends javax.swing.JFrame {

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
    }

    public void setConnection(Connection connection, String userName,
            String userPass) throws SQLException {

        if ((this.connection != null) && (!this.connection.isClosed())) {
            this.connection.close();
            System.out.println("LibraryMainFrame:  connectionClosed!!!");
        }
        this.connection = connection;
        System.out.println("LibraryMAinFrame:  setConnection!!!");
        Statement stmt;
        try {
            stmt = this.connection.createStatement();
            ResultSet rs = stmt.executeQuery("select distinct granted_role from user_role_privs");

            String userRole = "";
            System.out.println("LibraryMainFrame:  Role");

            while (rs.next()) {
                String role = rs.getString("granted_role");
                if (role.equalsIgnoreCase("LibrarianUserClient")) {
                    userRole = "Librarian";
                }

                if ((!userRole.equalsIgnoreCase("Librarian"))
                        && (role.equalsIgnoreCase("UserClient"))) {
                    userRole = "userClient";
                }
            }


            System.out.println("mainFrame: " + userRole);
            if (this.centerPanel!= null) {
                 this.remove(this.centerPanel);
            }
            if (userRole.equalsIgnoreCase("Librarian")) {
                System.out.println("LibrarianUserClient");
               
                this.centerPanel = new LibrarianUserPane(
                        new LibrarianUserClient(this.connection, userName,
                        userPass));
              //  this.add(centerPanel, BorderLayout.CENTER);
                this.revalidate();
            } else if (userRole.equalsIgnoreCase("userClient")) {
                System.out.println("UserClient");
                
                this.centerPanel = new NormalUserPane(new NormalUserClient(
                        this.connection, userName, userPass));
             //   this.add(centerPanel, BorderLayout.CENTER);
                this.revalidate();
                //centerPanel.repaint();

            } else {
            }
            
            this.jPanel2.add(centerPanel, BorderLayout.CENTER);
            jPanel2.revalidate();
            this.revalidate();
            this.setSize(this.getPreferredSize());
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        loginPane2 = new libraryproject.LoginPane(this);
        jPanel2 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(loginPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loginPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(260, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.WEST);

        jPanel2.setLayout(new java.awt.BorderLayout());
        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    
    private JPanel centerPanel = null;
    private Connection connection = null;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private libraryproject.LoginPane loginPane2;
    // End of variables declaration//GEN-END:variables
}
